/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.message.service.impl;

import com.alien.admin.modules.message.domain.NotifyAnnounce;
import com.alien.admin.utils.ValidationUtil;
import com.alien.admin.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.alien.admin.modules.message.repository.NotifyAnnounceRepository;
import com.alien.admin.modules.message.service.NotifyAnnounceService;
import com.alien.admin.modules.message.service.dto.NotifyAnnounceDto;
import com.alien.admin.modules.message.service.dto.NotifyAnnounceQueryCriteria;
import com.alien.admin.modules.message.service.mapstruct.NotifyAnnounceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.alien.admin.utils.PageUtil;
import com.alien.admin.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author zhouseyi
* @date 2021-11-17
**/
@Service
@RequiredArgsConstructor
public class NotifyAnnounceServiceImpl implements NotifyAnnounceService {

    private final NotifyAnnounceRepository notifyAnnounceRepository;
    private final NotifyAnnounceMapper notifyAnnounceMapper;

    @Override
    public Map<String,Object> queryAll(NotifyAnnounceQueryCriteria criteria, Pageable pageable){
        Page<NotifyAnnounce> page = notifyAnnounceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(notifyAnnounceMapper::toDto));
    }

    @Override
    public List<NotifyAnnounceDto> queryAll(NotifyAnnounceQueryCriteria criteria){
        return notifyAnnounceMapper.toDto(notifyAnnounceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public NotifyAnnounceDto findById(Integer id) {
        NotifyAnnounce notifyAnnounce = notifyAnnounceRepository.findById(id).orElseGet(NotifyAnnounce::new);
        ValidationUtil.isNull(notifyAnnounce.getId(),"NotifyAnnounce","id",id);
        return notifyAnnounceMapper.toDto(notifyAnnounce);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NotifyAnnounceDto create(NotifyAnnounce resources) {
        return notifyAnnounceMapper.toDto(notifyAnnounceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NotifyAnnounce resources) {
        NotifyAnnounce notifyAnnounce = notifyAnnounceRepository.findById(resources.getId()).orElseGet(NotifyAnnounce::new);
        ValidationUtil.isNull( notifyAnnounce.getId(),"NotifyAnnounce","id",resources.getId());
        notifyAnnounce.copy(resources);
        notifyAnnounceRepository.save(notifyAnnounce);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            notifyAnnounceRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<NotifyAnnounceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NotifyAnnounceDto notifyAnnounce : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发送者id", notifyAnnounce.getSenderId());
            map.put("消息标题", notifyAnnounce.getTitle());
            map.put("消息正文", notifyAnnounce.getMessage());
            map.put("是否html片段", notifyAnnounce.getIsHtml());
            map.put("跳转路径", notifyAnnounce.getRouter());
            map.put("类型:success/warning/info/error", notifyAnnounce.getType());
            map.put("显示时间: 毫秒，为0则不自动关闭", notifyAnnounce.getDuration());
            map.put("创建人", notifyAnnounce.getCreateBy());
            map.put("创建日期", notifyAnnounce.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
